package com.zxy.supplier_system.server.entity

import com.zxy.supplier_system.server.utils.GoodsRow
import jakarta.persistence.*
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import java.io.Serializable

@Entity
@DynamicUpdate
@DynamicInsert
@Table(
    uniqueConstraints = [
        UniqueConstraint(
            name = "check_inventory_record_record_id_goods_id_sub_goods_id_uindex",
            columnNames = ["record_id", "goods_id","sub_goods_id"]
        )
    ]
)
class CheckInventoryRecordItem {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Int?=null

    @ManyToOne(optional = false)
    @JoinColumn
    lateinit var goods: Goods

    @ManyToOne
    @JoinColumn
    var subGoods:SubGoods? = null

    @Column(nullable = false)
    var amount: Int = 0

    @ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = [])
    @JoinColumn(nullable = false)
    lateinit var record: CheckInventoryRecord

    var sequence: Int = 0

}

val CheckInventoryRecordItem.goodsExcelRow: GoodsRow
    get() = this.goods.excelRow.copy(amount=this.amount)
